package com.two.lbms.service;

import com.two.lbms.dao.AdminMapper;
import com.two.lbms.model.User;
import com.two.lbms.utils.AssertUtil;
import com.two.lbms.utils.LoginCaptchaUtil;
import com.two.lbms.utils.Md5Util;
import com.two.lbms.vo.Admin;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/7/30 21:15
 */
@Service
public class UserService {
    @Resource
    private AdminMapper adminMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    public User checkLogin(User user) {
        AssertUtil.isTrue(StringUtils.isBlank(user.getUsername()), "用户名不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(user.getPassword()),"密码不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(user.getCaptcha()),"验证码不能为空");
        String captchaCode = LoginCaptchaUtil.getCaptchaCode();
        AssertUtil.isTrue(!captchaCode.equalsIgnoreCase(user.getCaptcha()),"验证码错误");
        Admin admin = adminMapper.selectByAdminName(user.getUsername());
        AssertUtil.isTrue(admin==null,"用户名错误");
        AssertUtil.isTrue(admin.getAdminPassword().equals(Md5Util.encode(user.getPassword())),"密码错误");
        return user;

    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Admin queryUserByName(String admin) {
        return adminMapper.selectByAdminName(admin);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updatePassword(String admin, String oldPassword, String newPassword, String againPassword) {

        Admin user = adminMapper.selectByAdminName(admin);
        checkPasswordInfo(user, oldPassword, newPassword, againPassword);
        Admin newAdmin = new Admin();
        newAdmin.setId(user.getId());
        newAdmin.setAdminPassword(newPassword);
        // user.setUserPwd(Md5Util.encode(newPassword));
        AssertUtil.isTrue(adminMapper.updateByPrimaryKeySelective(newAdmin) < 1, "密码替换失败");

    }

    /**
     * 检查用户替换密码的信息是否正确
     *
     * @param user            用户对象
     * @param oldPassword     旧密码
     * @param newPassword     新密码
     * @param confirmPassword 确认密码
     */
    private void checkPasswordInfo(Admin user, String oldPassword, String newPassword, String confirmPassword) {
        AssertUtil.isTrue(null == user, "管理员不存在");
        AssertUtil.isTrue(!Objects.equals(user.getAdminPassword(), oldPassword), "旧密码错误");
        AssertUtil.isTrue(StringUtils.isBlank(oldPassword), "旧密码不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(newPassword), "新密码不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(confirmPassword), "确认密码不能为空");
        AssertUtil.isTrue(!Objects.equals(newPassword, confirmPassword), "确认密码与新密码不一致");
        AssertUtil.isTrue(Objects.equals(user.getAdminPassword(), newPassword), "新密码与原密码不能一致");

    }
}
